{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def evaluate(Y, labels_test, evaluate_method=1):\n",
    "    acc = 0\n",
    "    TP = 0\n",
    "    FN = 0\n",
    "    FP = 0\n",
    "    TN = 0\n",
    "    if evaluate_method == 1:\n",
    "        for predict_y, real_y in zip(Y, labels_test):\n",
    "            if predict_y == 12 and real_y == 'normal':\n",
    "                acc += 1\n",
    "            if predict_y != 12 and real_y != 'normal':\n",
    "                acc += 1\n",
    "            if predict_y == 12 and real_y != 'normal':\n",
    "                FN += 1\n",
    "            if real_y == 'normal' and predict_y !=12:\n",
    "                FP += 1\n",
    "            if predict_y != 12 and real_y != 'normal':\n",
    "                TP += 1\n",
    "            if predict_y == 12 and real_y == 'normal':\n",
    "                TN += 1\n",
    "    elif evaluate_method == 2:\n",
    "        for predict_y, real_y in zip(Y, labels_test):\n",
    "            if predict_y == 12 and real_y == 12:\n",
    "                acc += 1\n",
    "            if predict_y != 12 and real_y != 12:\n",
    "                acc += 1\n",
    "            if predict_y == 12 and real_y != 12:\n",
    "                FN += 1\n",
    "            if real_y == 12 and predict_y !=12:\n",
    "                FP += 1\n",
    "            if predict_y != 12 and real_y != 12:\n",
    "                TP += 1\n",
    "            if predict_y == 12 and real_y == 12:\n",
    "                TN += 1\n",
    "\n",
    "    precent_acc = acc / len(labels_test)\n",
    "    print(TP, FN)\n",
    "    print(FP, TN)\n",
    "    TPR = TP / (TP + FN)\n",
    "    FPR = FP / (FP + TN)\n",
    "    print('acc radio %s ' % precent_acc)\n",
    "    print('TPR is %s' % TPR)\n",
    "    print('FPR is %s' % FPR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "kddcup_data_columns = [\"duration\",\"protocol_type\",\"service\",\"flag\",\"src_bytes\",\n",
    "            \"dst_bytes\",\"land_f\",\"wrong_fragment\",\"urgent\",\"hot\",\"num_failed_logins\",\n",
    "            \"logged_in\",\"num_compromised\",\"root_shell\",\"su_attempted\",\"num_root\",\n",
    "            \"num_file_creations\",\"num_shells\",\"num_access_files\",\"num_outbound_cmds\",\n",
    "            \"is_host_login\",\"is_guest_login\",\"count\",\"srv_count\",\"serror_rate\",\n",
    "            \"srv_serror_rate\",\"rerror_rate\",\"srv_rerror_rate\",\"same_srv_rate\",\n",
    "            \"diff_srv_rate\",\"srv_diff_host_rate\",\"dst_host_count\",\"dst_host_srv_count\",\n",
    "            \"dst_host_same_srv_rate\",\"dst_host_diff_srv_rate\",\"dst_host_same_src_port_rate\",\n",
    "            \"dst_host_srv_diff_host_rate\",\"dst_host_serror_rate\",\"dst_host_srv_serror_rate\",\n",
    "            \"dst_host_rerror_rate\",\"dst_host_srv_rerror_rate\",\"labels\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.externals import joblib\n",
    "\n",
    "# no_pca_svm_model\n",
    "no_pca_clf = joblib.load('./model/NO_PCA_IDS_model.m')\n",
    "# pca_svm_model\n",
    "pca_clf = joblib.load('./model/IDS_model_8-0.m')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "kddcup_data = pd.read_csv('./data/kddcup_data.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>duration</th>\n",
       "      <th>protocol_type</th>\n",
       "      <th>service</th>\n",
       "      <th>flag</th>\n",
       "      <th>src_bytes</th>\n",
       "      <th>dst_bytes</th>\n",
       "      <th>land_f</th>\n",
       "      <th>wrong_fragment</th>\n",
       "      <th>urgent</th>\n",
       "      <th>hot</th>\n",
       "      <th>...</th>\n",
       "      <th>dst_host_srv_count</th>\n",
       "      <th>dst_host_same_srv_rate</th>\n",
       "      <th>dst_host_diff_srv_rate</th>\n",
       "      <th>dst_host_same_src_port_rate</th>\n",
       "      <th>dst_host_srv_diff_host_rate</th>\n",
       "      <th>dst_host_serror_rate</th>\n",
       "      <th>dst_host_srv_serror_rate</th>\n",
       "      <th>dst_host_rerror_rate</th>\n",
       "      <th>dst_host_srv_rerror_rate</th>\n",
       "      <th>labels</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>tcp</td>\n",
       "      <td>http</td>\n",
       "      <td>SF</td>\n",
       "      <td>162</td>\n",
       "      <td>4528</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>tcp</td>\n",
       "      <td>http</td>\n",
       "      <td>SF</td>\n",
       "      <td>236</td>\n",
       "      <td>1228</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>2</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.50</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>tcp</td>\n",
       "      <td>http</td>\n",
       "      <td>SF</td>\n",
       "      <td>233</td>\n",
       "      <td>2032</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.33</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>tcp</td>\n",
       "      <td>http</td>\n",
       "      <td>SF</td>\n",
       "      <td>239</td>\n",
       "      <td>486</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.25</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>tcp</td>\n",
       "      <td>http</td>\n",
       "      <td>SF</td>\n",
       "      <td>238</td>\n",
       "      <td>1282</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>5</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.20</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1048599</th>\n",
       "      <td>0</td>\n",
       "      <td>icmp</td>\n",
       "      <td>urh_i</td>\n",
       "      <td>SF</td>\n",
       "      <td>83</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.48</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1048600</th>\n",
       "      <td>0</td>\n",
       "      <td>icmp</td>\n",
       "      <td>red_i</td>\n",
       "      <td>SF</td>\n",
       "      <td>91</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.02</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.11</td>\n",
       "      <td>0.0</td>\n",
       "      <td>normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1048601</th>\n",
       "      <td>0</td>\n",
       "      <td>icmp</td>\n",
       "      <td>urh_i</td>\n",
       "      <td>SF</td>\n",
       "      <td>36</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>20</td>\n",
       "      <td>0.20</td>\n",
       "      <td>0.07</td>\n",
       "      <td>0.20</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1048602</th>\n",
       "      <td>0</td>\n",
       "      <td>tcp</td>\n",
       "      <td>harvest</td>\n",
       "      <td>REJ</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.85</td>\n",
       "      <td>1.0</td>\n",
       "      <td>satan</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1048603</th>\n",
       "      <td>0</td>\n",
       "      <td>icmp</td>\n",
       "      <td>tim_i</td>\n",
       "      <td>SF</td>\n",
       "      <td>564</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>pod</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1048604 rows × 42 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         duration protocol_type  service flag  src_bytes  dst_bytes  land_f  \\\n",
       "0               0           tcp     http   SF        162       4528       0   \n",
       "1               0           tcp     http   SF        236       1228       0   \n",
       "2               0           tcp     http   SF        233       2032       0   \n",
       "3               0           tcp     http   SF        239        486       0   \n",
       "4               0           tcp     http   SF        238       1282       0   \n",
       "...           ...           ...      ...  ...        ...        ...     ...   \n",
       "1048599         0          icmp    urh_i   SF         83          0       0   \n",
       "1048600         0          icmp    red_i   SF         91          0       0   \n",
       "1048601         0          icmp    urh_i   SF         36          0       0   \n",
       "1048602         0           tcp  harvest  REJ          0          0       0   \n",
       "1048603         0          icmp    tim_i   SF        564          0       0   \n",
       "\n",
       "         wrong_fragment  urgent  hot  ...  dst_host_srv_count  \\\n",
       "0                     0       0    0  ...                   1   \n",
       "1                     0       0    0  ...                   2   \n",
       "2                     0       0    0  ...                   3   \n",
       "3                     0       0    0  ...                   4   \n",
       "4                     0       0    0  ...                   5   \n",
       "...                 ...     ...  ...  ...                 ...   \n",
       "1048599               0       0    0  ...                   1   \n",
       "1048600               0       0    0  ...                   4   \n",
       "1048601               0       0    0  ...                  20   \n",
       "1048602               0       0    0  ...                   1   \n",
       "1048603               0       0    0  ...                   1   \n",
       "\n",
       "         dst_host_same_srv_rate  dst_host_diff_srv_rate  \\\n",
       "0                          1.00                    0.00   \n",
       "1                          1.00                    0.00   \n",
       "2                          1.00                    0.00   \n",
       "3                          1.00                    0.00   \n",
       "4                          1.00                    0.00   \n",
       "...                         ...                     ...   \n",
       "1048599                    0.00                    0.48   \n",
       "1048600                    0.03                    0.02   \n",
       "1048601                    0.20                    0.07   \n",
       "1048602                    0.00                    1.00   \n",
       "1048603                    1.00                    0.00   \n",
       "\n",
       "         dst_host_same_src_port_rate  dst_host_srv_diff_host_rate  \\\n",
       "0                               1.00                          0.0   \n",
       "1                               0.50                          0.0   \n",
       "2                               0.33                          0.0   \n",
       "3                               0.25                          0.0   \n",
       "4                               0.20                          0.0   \n",
       "...                              ...                          ...   \n",
       "1048599                         0.00                          0.0   \n",
       "1048600                         0.03                          0.0   \n",
       "1048601                         0.20                          0.0   \n",
       "1048602                         0.00                          0.0   \n",
       "1048603                         1.00                          0.0   \n",
       "\n",
       "         dst_host_serror_rate  dst_host_srv_serror_rate  dst_host_rerror_rate  \\\n",
       "0                         0.0                       0.0                  0.00   \n",
       "1                         0.0                       0.0                  0.00   \n",
       "2                         0.0                       0.0                  0.00   \n",
       "3                         0.0                       0.0                  0.00   \n",
       "4                         0.0                       0.0                  0.00   \n",
       "...                       ...                       ...                   ...   \n",
       "1048599                   0.0                       0.0                  0.00   \n",
       "1048600                   0.0                       0.0                  0.11   \n",
       "1048601                   0.0                       0.0                  0.00   \n",
       "1048602                   0.1                       0.0                  0.85   \n",
       "1048603                   0.0                       0.0                  0.00   \n",
       "\n",
       "         dst_host_srv_rerror_rate  labels  \n",
       "0                             0.0  normal  \n",
       "1                             0.0  normal  \n",
       "2                             0.0  normal  \n",
       "3                             0.0  normal  \n",
       "4                             0.0  normal  \n",
       "...                           ...     ...  \n",
       "1048599                       0.0  normal  \n",
       "1048600                       0.0  normal  \n",
       "1048601                       0.0  normal  \n",
       "1048602                       1.0   satan  \n",
       "1048603                       0.0     pod  \n",
       "\n",
       "[1048604 rows x 42 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kddcup_data = kddcup_data.drop('Unnamed: 0', axis=1)\n",
    "kddcup_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "kddcup_data_labels = kddcup_data['labels']\n",
    "kddcup_data_features = kddcup_data.drop('labels', axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "protocol_type = kddcup_data_features['protocol_type']\n",
    "service = kddcup_data_features['service']\n",
    "flag = kddcup_data_features['flag']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.01853028,  0.40790156, -0.29461112, ..., -0.49620316,\n",
       "        -0.16677597, -0.1623782 ],\n",
       "       [-0.01853028,  0.40790156, -0.29461112, ..., -0.49620316,\n",
       "        -0.16677597, -0.1623782 ],\n",
       "       [-0.01853028,  0.40790156, -0.29461112, ..., -0.49620316,\n",
       "        -0.16677597, -0.1623782 ],\n",
       "       ...,\n",
       "       [-0.01853028, -1.69608188,  2.67218356, ..., -0.49620316,\n",
       "        -0.16677597, -0.1623782 ],\n",
       "       [-0.01853028,  0.40790156, -0.44295086, ..., -0.49620316,\n",
       "         5.46330223,  6.45018381],\n",
       "       [-0.01853028, -1.69608188,  2.52384382, ..., -0.49620316,\n",
       "        -0.16677597, -0.1623782 ]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn import preprocessing\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "# 使用独热编码对字符型离散变量进行处理\n",
    "le = preprocessing.LabelEncoder()\n",
    "enc = preprocessing.OneHotEncoder()\n",
    "lb = preprocessing.LabelBinarizer()\n",
    "\n",
    "# 对训练集进行 one-hot 编码\n",
    "kddcup_data_features['protocol_type'] = le.fit_transform(protocol_type)\n",
    "kddcup_data_features['service'] = le.fit_transform(service)\n",
    "kddcup_data_features['flag'] = le.fit_transform(flag)\n",
    "\n",
    "X = kddcup_data_features\n",
    "standard_train_X = StandardScaler().fit_transform(X)\n",
    "standard_train_X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1048604, 41)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "standard_train_X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "444074 8712\n",
      "17142 578676\n",
      "acc radio 0.9753443626001808 \n",
      "TPR is 0.9807591224110286\n",
      "FPR is 0.028770530598269943\n"
     ]
    }
   ],
   "source": [
    "Y = no_pca_clf.predict(standard_train_X)\n",
    "evaluate(Y, kddcup_data_labels, evaluate_method=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "pca = joblib.load('./model/pca_model.m')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.92900974e-01 1.29485682e-01 7.04439291e-02 5.31449808e-02\n",
      " 4.85815317e-02 4.17691433e-02 3.96529926e-02 3.28079685e-02\n",
      " 2.80512201e-02 2.73051427e-02 2.64398260e-02 2.51755671e-02\n",
      " 2.50034191e-02 2.49993420e-02 2.48596994e-02 2.46763455e-02\n",
      " 2.32500385e-02 2.25602201e-02 1.92203509e-02 1.75161808e-02\n",
      " 1.55146912e-02 1.50089410e-02 1.22424414e-02 1.11758835e-02\n",
      " 1.03959990e-02 8.92111257e-03 7.85233782e-03 5.90036869e-03\n",
      " 3.48647746e-03 3.29727876e-03 2.46079181e-03 1.64211863e-03\n",
      " 1.22207487e-03 1.08234470e-03 7.21889664e-04 5.30475917e-04\n",
      " 3.59285624e-04 2.25140197e-04 1.02027978e-04 1.37640849e-05\n",
      " 1.10702605e-33]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(1048604, 17)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(pca.explained_variance_ratio_)\n",
    "pca_kddcup_features = pca.fit_transform(standard_train_X)\n",
    "pca_kddcup_features = pca_kddcup_features[:, :17]\n",
    "pca_kddcup_features.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "205545 247241\n",
      "119197 476621\n",
      "acc radio 0.6505468222512979 \n",
      "TPR is 0.4539561735566029\n",
      "FPR is 0.20005605738665164\n"
     ]
    }
   ],
   "source": [
    "Y = pca_clf.predict(pca_kddcup_features)\n",
    "evaluate(Y, kddcup_data_labels, evaluate_method=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
